#include "map.h"
#include <windows.h>
#include <iostream>
#include <stdio.h>
#include <conio.h>
#include <time.h>

#define SPACE 32
using namespace std;
#define INITIAL_WAIT 1000
#define START_STATE 0
#define PLAY_STATE 1
#define END_STATE 2
#define DIFFICULTY_TIME_DECREASE 100	//The amount less time (ms) between falls for each level of difficulty ~Garth

void printIntro()
{
	printf("Welcome to Tetris!\n");
	printf("TODO: type in your name here!\n\n\n");

	printf("Instructions:	\n");
	printf("Player 1:            Player 2:			\n");
	printf("       W                    I			\n");
	printf("     (flip)               (flip)		\n");
	printf("       |                    |			\n");
	printf(" A <--   --> D        J <--   --> L	\n");
	printf("       |                    |			\n");	
	printf("       V                    V			\n");
	printf("       S                    K			\n");
	printf("Slam Key: Space             H           \n");
	printf("Press any key to continue \nor [space] quit.\n\n");
}

int main(int argc, char ** argv)
{
	int gameState = 0;
    // intialize
	//set up the time	
	time_t now, then = clock(), passed;	//clock() gives us a better resolution timer than time().	~Garth

	int timeBetweenFalls = INITIAL_WAIT;		//Will double as our throttle code/difficulty ramp up code.  Gross.~Garth

	//is this the first piece going through the game?
	bool justStarted = true;
	bool running = true;
	char input = NULL;
	//variable for the intro menu
	char doit;
	bool goingDown = false;
	COORD p = {0,1};
	
	tetrisMap map(0);	//These paramaters represent which collumn of the console to start drawing them at. ~Garth
	tetrisMap map2(30);

	while(running)
	{
		switch(gameState)
		{
			//if it is the start state then display the menu
		case START_STATE:
			system("cls");
			printIntro();			
			doit = _getch();
			system("cls");
			gameState = PLAY_STATE;
			break;
		case PLAY_STATE:
			// reset the cursor to 0,0 (start drawing at the top)
			SetConsoleCursorPosition(
			GetStdHandle(STD_OUTPUT_HANDLE), p);
			//display the map (draw)				
			map.displayMap();
			map2.displayMap();
			//display the "next" piece
			map.displayNextMap();
			map2.displayNextMap();
			//if we just started then create a piece, the game(map.cpp) will handle creating pieces from here on out
			if(justStarted)
			{
				map.createPiece();
				map2.createPiece();
				justStarted = false;
			}
			//grab what time it is now
			now = clock();
			passed = now - then;
			//check to see if it is greater than wait
			if(passed > timeBetweenFalls)
			{
				map.downShift();
				map2.downShift();
				then = now;
			}
			timeBetweenFalls = INITIAL_WAIT - map.getDifficulty() * DIFFICULTY_TIME_DECREASE;	//Shortens the time between falls based on the current difficulty level ~Garth
			//check to see if we lost the game
			if(map.checkLoss() || map2.checkLoss())
			{
				gameState = END_STATE;
			}
			//if a key was hit then handle the input
			if(_kbhit())
			{
				input = _getch();
				switch(input)
				{
					//Player 1 inputs
				case 'a':
					map.leftShift(); input = NULL; break;
				case 'w':
					map.flipShape(); input = NULL; break;
				case 's':
					map.downShift(); input = NULL; break;
				case 'd': 
					map.rightShift(); input = NULL; break;
					//Player 2 inputs
				case 'j':
					map2.leftShift(); input = NULL; break;
				case 'i':
					map2.flipShape(); input = NULL; break;
				case 'k':
					map2.downShift(); input = NULL; break;
				case 'l': 
					map2.rightShift(); input = NULL; break;
				case SPACE:
					//running = false; input = NULL; break;
					map.slamPiece(); input = NULL; break;
				case 'h':
					map2.slamPiece(); input = NULL; break;
				}
			}
			break;
			//if the game is over then go to the end state
		case END_STATE:
			system("cls");
			printf("Oh no you lost!\n dont worry, there isnt really a win condition on this game...\nUnless you wanna keep playing forever..\n");
			printf("Wanna try again?, hit y for yes, anything else will terminate the game...\n");
			input = _getch();
			//if the person wants to play again reset everything and start the game over....
			if(input == 'y')
			{
				gameState = START_STATE;
				map.clearStatic();
				map2.clearStatic();
				map.clearMap();
				map2.clearMap();
				justStarted = true;
			}
			//....otherwise end the game
			else {running = false;}
			break;
		}

	}

	return 0;
}

